/**
 * 2011-11-14下午9:26:11
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.MysqlConn;
import bean.Goods;

/**
 * @author Baicai
 * 
 *         从数据库获取主页显示数据的方法类
 */
public class IndexViewMethod {

	/**
	 * 从数据库获取最后的10条数据
	 * 
	 * @return List<Goods>
	 */
	public static List<Goods> getListGoods() {
		List<Goods> list = new ArrayList<Goods>();
		try {
			ResultSet resultSet;
			MysqlConn
					.setSqlString("select goods_id,goods_name,goods_sort,goods_price,goods_information "
							+ "from goods where goods_state=1 order by goods_id desc limit 20");// 查询最后十条记录中需要的信息
			resultSet = MysqlConn.queryDB();// 以resultSet获取查询结果
			while (resultSet.next()) {
				Goods goods = new Goods();// 以Goods对象获取结果集中得查询结果
				goods.setGoodsId(Integer.parseInt(resultSet
						.getString("goods_id")));
				goods.setGoodsName(resultSet.getString("goods_name"));
				goods.setGoodsSort(resultSet.getString("goods_sort"));
				goods.setGoodsPrice(Float.valueOf(resultSet
						.getString("goods_price")));
				goods.setGoodsInformation(resultSet
						.getString("goods_information"));

				list.add(goods);// 将goods放入list
			}
			return list;// 返回得到的list
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			MysqlConn.free();
		}
		return null;
	}
}
